//
//  PurchaseNowTableViewCellTwo.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/4/18.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "PurchaseNowTableViewCellTwo.h"
#import "SKTag.h"
#import "SKTagView.h"
#import "ProdcutDetaiModel.h"
#import "SKTagButton.h"
@interface PurchaseNowTableViewCellTwo()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons;
@property(nonatomic, strong) NSArray *options;
@end

@implementation PurchaseNowTableViewCellTwo

- (void)awakeFromNib {
    // Initialization code
    self.heightCons.constant = .33f;
}

- (void)updateWithOptionValues:(NSArray *)options {
    self.options = options;
    self.tagView.preferredMaxLayoutWidth = SCREEN_WIDTH - 16.f;
    self.tagView.padding = UIEdgeInsetsMake(12, 12, 12, 12);
    self.tagView.interitemSpacing = 15;
    self.tagView.lineSpacing = 10;
    [self.tagView removeAllTags];
    
    for (ProductOptionValueModel *valueModel in options) {
        SKTag *tag = [SKTag tagWithText:valueModel.name];
        tag.textColor = [UIColor blackColor];
        tag.fontSize = 14;
        tag.bgColor = [UIColor colorWithHexString:@"ebebeb"];
        tag.padding = UIEdgeInsetsMake(5, 12, 5, 12);
        tag.cornerRadius = 15;
        tag.borderColor=[UIColor clearColor];
        tag.borderWidth=0.5;
        [self.tagView addTag:tag];
    }
    self.tagView.didTapTagAtIndex = ^(NSUInteger index) {
        ProductOptionValueModel *model = [self.options objectAtIndex:index];
        model.checked = !model.checked;
        SKTagButton *tag = (SKTagButton *)[[self.tagView subviews] objectAtIndex:index];
        [self setTagView:tag checked:model.checked];
        for (int i = 0; i < self.options.count; i ++) {
            if (index != i) {
                SKTagButton *tag = (SKTagButton *)[[self.tagView subviews] objectAtIndex:i];
                [self setTagView:tag checked:NO];
            }
        }
        if (self.selectOptionBlock && model.checked) {
            self.selectOptionBlock(model);
        } else {
            self.selectOptionBlock(nil);
        }
    };
}

- (void)setTagView:(SKTagButton *)btn checked:(BOOL) checked {
    if (checked) {
        [btn setBackgroundColor:[UIColor colorWithHexString:@"f35959"]];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        [btn setBackgroundColor:[UIColor colorWithHexString:@"ebebeb"]];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
